package com.cn.xc.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * FUN: date tools class
 * 
 * @author sushy
 * @date 2016-3-23
 */
public class DateUtil {

	public final static String YYYYMMDD = "yyyyMMdd";
	public final static String YYYYMMDDHH24MISS = "yyyyMMddHHmmss";
	public final static String HH24MISS = "HHmmss";

	/**
	 *  //# acquire current date, format='yyyyMMddHHmmss'
	 * 
	 * @return
	 */
	public static String getCurDate() {
		return date2Str(new Date(), YYYYMMDDHH24MISS);
	}

	/**
	 *  //# acquire current date, custom time format
	 * 
	 * @return
	 */
	public static String getCurDate(String dateFormat) {
		return date2Str(new Date(), dateFormat);
	}

	/**
	 *  //# date-string convert to date type
	 *   
	 * @param dateSrc
	 * @return
	 */
	public static Date str2Date(String dateSrc) {
		return str2Date(dateSrc, YYYYMMDDHH24MISS);
	}

	/**
	 * //# date-string convert to date type, according to custom format
	 * 
	 * @param dateSrc
	 * @param dateFormat
	 * @return
	 */
	public static Date str2Date(String dateSrc, String dateFormat) {
		if (StringUtil.isEmpty(dateSrc)) {
			return null;
		}

		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		try {
			return df.parse(dateSrc);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 *  //# date convert to string
	 * 
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String date2Str(Date date) {
		return date2Str(date, YYYYMMDDHH24MISS);
	}

	/**
	 * //# date convert to string according to custom format
	 * 
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String date2Str(Date date, String dateFormat) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(date);
	}

}
